import { saveAs } from 'file-saver'
import {
  SavePlanTasks,
  GetPlanTasksList,
  DeletePlanTasks,
  GetPlanTasksDetail,
  UpdatePlanTasks
} from '@/api/demandManage/index.js'
import {
  PriorityOptions,
  whetherOption,
  missionTypeOption,
  targetShapeOption,
  weatherConditionOption
} from '@/assets/data/pathInfo'
import { ExportPlanTasks } from '@/api/decisionManage/index.js'
import { resolutionOption } from "@/assets/data/pathInfo"
export default {
  components: {},
  data() {
    return {
      formInline: {
        demand: undefined,
        targetArea: undefined,
        sample: undefined,
        times: undefined,
        rwlx: undefined,
        pageCurrent: 1,
        pageSize: 10
      },
      total: 0,
      options: [
        {
          value: '值1',
          label: '选项1'
        },
        {
          value: '值2',
          label: '选项2'
        }
      ],
      tableData: [],
      dialogVisible: false,
      labelPosition: 'right',
      formLabelAlign: {
        name: '',
        region: '',
        type: ''
      },
      taskCaseBool: 0,
      PriorityOptions,
      whetherOption,
      missionTypeOption,
      targetShapeOption,
      weatherConditionOption,
      tableLoading: false,
      rules: {
        rwlx: [
          { required: true, message: '请选择任务类型', trigger: 'change' }
        ],
        mbxt: [
          { required: true, message: '请选择目标形态', trigger: 'change' }
        ],
        qxtj: [
          {
            required: true,
            message: '请选择气象条件',
            trigger: 'change'
          }
        ],
        gjys: [
          { required: true, message: '请选择攻击因素', trigger: 'change' }
        ],
        sfhzmb: [
          { required: true, message: '请选择合作目标', trigger: 'change' }
        ],
        zhlx: [{ required: true, message: '请选择载荷类型', trigger: 'change' }],
        gzms: [{ required: true, message: '请输入工作模式', trigger: 'blur' }],
        fbl: [{ required: true, message: '请输入分辨率', trigger: 'blur' }],
        yxj: [{ required: true, message: '请选择优先级', trigger: 'change' }],
        xqmc: [
          { required: true, message: '请输入需求名称', trigger: 'blur' }
        ]
      },
      zhlxOptions: [
        { label: '光学', value: '光学' },
        { label: 'SAR', value: 'SAR' },
      ],
      resolutionOption
    }
  },
  computed: {},
  created() { },
  mounted() {
    this.GetPlanTasksList()
  },
  methods: {
    /** 导出观测需求 */
    async handleExportObservation() {
      this.$message.warning("正在导出")
      try {
        const response = await ExportPlanTasks()
        console.log(response)
        if (response.status !== 200) {
          return this.$message.error("操作失败")
        }
        saveAs(response.data, '观测需求决策数据.xlsx')
      } catch (error) {
        console.error('下载Excel失败:', error)
      }
    },
    onSubmit() {
      this.formInline.pageCurrent = 1
      this.GetPlanTasksList()
    },
    onReset() {
      this.formInline.pageCurrent = 1
      this.formInline = {}
      this.GetPlanTasksList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.formInline.pageCurrent = val
      this.GetPlanTasksList()
    },
    /**
     * 默认为新建任务案例
     * @param val
     */
    addNewTaskCase(val) {
      this.dialogVisible = true
      this.taskCaseBool = val
      if (val == 1) {
        this.formLabelAlign = {}
      }
    },
    /** 获取任务筹划 */
    GetPlanTasksList() {
      this.tableLoading = true
      GetPlanTasksList(this.formInline).then(resposne => {
        if (resposne.data.code == 200) {
          this.tableData = resposne.data.data.records
          this.total = resposne.data.data.total
        } else {
          this.$message({
            type: 'error',
            message: '获取失败'
          })
        }
        this.tableLoading = false
      })
    },
    /** 保存任务筹划 */
    SaveAndPlanTasks(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false
        this.dialogVisible = false
        this.taskCaseBool = 0
        const gcxq = {
          ...this.formLabelAlign
        }
        SavePlanTasks(gcxq).then(resposne => {
          if (resposne.data.code == 200) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.GetPlanTasksList()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败'
            })
          }
          this.formLabelAlign = {}
        })
      })
    },
    /** 删除任务筹划 */
    DeletePlanTasks(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const idParam = {
            ids: [row.xqid]
          }
          DeletePlanTasks(idParam).then(resposne => {
            if (resposne.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this.GetPlanTasksList()
            } else {
              this.$message({
                type: 'error',
                message: '删除失败'
              })
            }
          })
        })
        .catch(() => {
          console.log('Cancel')
        })
    },
    /** 获取任务详情 */
    GetPlanTasksDetail(row) {
      this.addNewTaskCase(0)
      GetPlanTasksDetail(row.xqid).then(resposne => {
        if (resposne.data.code == 200) {
          this.formLabelAlign = resposne.data.data
        } else {
          this.$message({
            type: 'error',
            message: '获取失败'
          })
        }
      })
    },
    /** 编辑任务 */
    UpdatePlanTasks(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false

        UpdatePlanTasks(this.formLabelAlign).then(resposne => {
          if (resposne.data.code == 200) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.GetPlanTasksList()
            this.dialogVisible = false
          } else {
            this.$message({
              type: 'error',
              message: '操作失败'
            })
          }
        })
      })
    },
    /** 关闭弹窗，清除校验 */
    handleCloseModal() {
      this.$refs.ruleForm.clearValidate()
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false
    }
  }
}
